package com.quan.system.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.core.MyBaseServiceImpl;
import com.quan.system.entity.SystemMenu;
import com.quan.system.mapper.SystemMenuMapper;
import com.quan.system.service.SystemMenuService;
import com.quan.system.service.SystemRolePermissionService;

/**
 * <p>
 * 通用用户权限系统-菜单表 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-03
 */
@Service
public class SystemMenuServiceImpl extends MyBaseServiceImpl<SystemMenuMapper, SystemMenu> implements SystemMenuService {
	
	@Autowired
	private SystemRolePermissionService rolePermissionService ;
	
	@Override
	public boolean removeById(Serializable id) {
		return this.recursiveDelete(id);
	}
	
	private boolean recursiveDelete(Serializable id) {
		QueryWrapper<SystemMenu> queryWrapper = new QueryWrapper<SystemMenu>();
		queryWrapper.eq("pid", id);
		
		List<SystemMenu> child = super.list(queryWrapper) ;
		if(null != child) {
			for (SystemMenu entity : child) {
				recursiveDelete(entity.getId());
			}
		}
		
		//删除与角色关联的数据
		this.rolePermissionService.deleteByMenuId(id);
		
		return super.removeById(id);
	}

}
